#include "playscene.h"
#include <QDebug>
#include <QString>
#include <QMenuBar>
#include <QPainter>
#include <QTimer>
#include "MyPushButton.h"
#include <QLabel>
#include <mycoin.h>
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>

//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}

PlayScene::PlayScene(int laveInd){

    this->laveIndex = laveInd;
    QString str = QString("进入了第%1关\n").arg(laveInd);
    qDebug()<<str;

    //初始化游戏场景
    //设置固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("翻金币场景");

    //创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");

    //创建退出 菜单项
    QAction *  quitAction = startMenu->addAction("退出");

    //返回按钮音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
    //翻金币音效
    QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this);
    //胜利按钮音效
    QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);





    //点击退出 实现退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png" , ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width() , this->height() - backBtn->height());

    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=](){
        qDebug() << "翻金币场景中：点击了返回按钮";
        backSound->play();
        QTimer::singleShot(300,this,[=](){
            emit this->chooseSceneBack();
        });

    });

    QLabel* winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move( (this->width() - tmpPix.width())*0.5 , -tmpPix.height());


    //显示当前关卡数
    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString str1 = QString("Level: %1").arg(this->laveIndex);
    //将字体设置到标签控件中
    label->setFont(font);
    label->setText( str1);
    label->setGeometry(30, this->height() - 50,120, 50);

    dataConfig config;
    for(int i = 0; i<4; ++i){
        for(int j = 0; j<4; ++j){
            this->gameArray[i][j] = config.mData[this->laveIndex][i][j];
        }
    }

    //创建金币的背景图片
    for(int i = 0 ; i < 4;i++)
    {
       for(int j = 0 ; j < 4; j++)
       {
          //绘制背景图片
           QLabel* label = new QLabel;
           label->setGeometry(0,0,50,50);
           label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
           label->setParent(this);
           label->move(57 + i*50,200+j*50);
           //
           MyCoin *coin = new MyCoin((gameArray[i][j] == 1)?":/res/Coin0001.png":":/res/Coin0008.png");
           coin->setParent(this);
           coin->move(59 + i*50,204+j*50);

           coin->setX(i);
           coin->setY(j);
           coin->flag = this->gameArray[i][j];

           //金币放入
           coinBtn[i][j] = coin;

           //点击金币翻转
           connect(coin, &MyCoin::clicked,[=](){
                flipSound->play();
                coin->changFlag();
                gameArray[i][j] = (gameArray[i][j] == 0) ? 1 : 0;

                //翻转周围
//              QTimer::singleShot(300,this, [=](){
                  if(coin->getX()+1 <= 3){
                      coinBtn[coin->getX()+1][coin->getY()]->changFlag();
                      gameArray[coin->getX()+1][coin->getY()] = (gameArray[coin->getX()+1][coin->getY()] == 0) ? 1 : 0;
                  }
                  if(coin->getX()-1 >= 0){
                      coinBtn[coin->getX()-1][coin->getY()]->changFlag();
                      gameArray[coin->getX()-1][coin->getY()] = (gameArray[coin->getX()-1][coin->getY()] == 0) ? 1 : 0;
                  }
                  if(coin->getY()-1 >= 0){
                      coinBtn[coin->getX()][coin->getY()-1]->changFlag();
                      gameArray[coin->getX()][coin->getY()-1] = (gameArray[coin->getX()][coin->getY()-1] == 0) ? 1 : 0;
                  }
                  if(coin->getY()+1 <= 3){
                      coinBtn[coin->getX()][coin->getY()+1]->changFlag();
                      gameArray[coin->getX()][coin->getY()+1] = (gameArray[coin->getX()][coin->getY()+1] == 0) ? 1 : 0;
                  }
//              });

              //判断是否胜利
              this->isWin = true;
              for(int i = 0;i<4;++i){
                  for(int  j = 0;j<4;++j){
                      if(coinBtn[i][j]->flag == false){
                          this->isWin = false;
//                          break;
                      }
                      qDebug()<<coinBtn[i][j]->flag;
                  }
              }

              if(this->isWin == true){
                  qDebug()<<"胜利";
                  winSound->play();
                  for(int i = 0;i<4;++i){
                      for(int  j = 0;j<4;++j){
                          coinBtn[i][j]->isWin = true;
                      }
                  }

                  QPropertyAnimation * animation1 =  new QPropertyAnimation(winLabel,"geometry");
                  animation1->setDuration(1000);
                  animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                  animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                  animation1->setEasingCurve(QEasingCurve::OutBounce);
                  animation1->start();
              }

           });

       }
    }

}

void PlayScene::paintEvent(QPaintEvent *event){
    //创建背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);
}
